package com.team.controller;

import com.team.entity.AppVersion;
import com.team.service.AppVersionService;
import com.team.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
public class AppVersionController {
    @Autowired
    private AppVersionService appVersionService;
    @RequestMapping(value = "addAppVersion")
    public String addAppVersion(AppVersion appVersion,
                                @RequestParam(value = "a_downloadlink" ,required = false)CommonsMultipartFile file,
                                HttpServletRequest request){
        System.out.println(appVersion.getVersionsize());

        try {
            String realPath = request.getServletContext().getRealPath("jsp/upload/apk");
            String uploadFile = FileUploadUtil.uploadFile(realPath, file);

            appVersion.setApkfilename(file.getOriginalFilename());
            appVersion.setDownloadlink("jsp/upload/apk/"+uploadFile);
            appVersion.setApklocpath(realPath+"\\"+uploadFile);
            appVersionService.addAppVersion(appVersion);

            return "redirect:getAppVersion?id="+appVersion.getAppid();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }



}
